$(function(){
    getThatDayItemList();

    $('#side-menu ul').eq(2).addClass('in');


    $('#btn_search').on('click',function(){
        $("#pageNum").val(1);
        getThatDayItemList();
    });

    $(document).keydown(function(event){
        if(event.keyCode==13){
            $("#btn_search").click();
        }
    });
});



/**
 *  功能描述：获取信息列表
 *  请求方式：GET
 *  请求地址：/api/item/list
 *  函数名称：getThatDayItemList
 */

function getThatDayItemList(){
    $.ajax({
        url: whole_path+'/api/item/list',
        type: 'GET',
        dataType: 'json',
        cache:false,
        data: $('#item_form').serialize(),
        beforeSend:function(data){
            $.progressBar({message:'<p>正在努力加载数据...</p>',modal:true,canCancel:true});
        },
        success:function(data){
            if(data.status == 0) {
                var json = data.data;
                var list = json.result;
                var temp = "";
                if (list != null && list.length>0){
                    $.each(list, function (index, item) {
                        var content = '';
                        if(item.content != null){
                            content = item.content.toString();
                            if (content.length > 10 && content != 0){
                                content = content.substring(0,10)+"...";
                            }else {
                                content
                            }
                        }
                       temp += '<tr>'
                            + '<td data-title="发布人">' + item.realName + '</td>'
                            + '<td data-title="出发地">' + item.startProvince+item.startCity+(item.startCounty==null?'':item.startCounty) + '</td>'
                            + '<td data-title="目的地">' + item.destProvince+item.destCity+(item.destCounty==null?'':item.destCounty ) + '</td>'
                            + '<td data-title="联系电话">' + item.phoneNumber + '</td>'
                            + '<td data-title="装货地址">' + (item.loadAddress==null?'':item.loadAddress) + '</td>'
                            + '<td data-title="卸货地址">' + (item.unloadAddress==null?'':item.unloadAddress) + '</td>'
                            + '<td data-title="发布内容" style="color:#0b94ea;max-width:200px;white-space:nowrap; overflow:hidden; text-overflow:ellipsis" data-container="body" data-toggle="popover" data-trigger="hover" data-placement="top" data-content="'+item.content+'">' + content + '</td>'
                            + '<td data-title="发布时间">' + timestampFormat(item.pushTime) + '</td>'
                            + '<td data-title="现有车辆数">' + item.receiveCount + '</td>'
                            + '</tr>';
                    })
                    $('#item_list tbody').html(temp);
                    $("[data-toggle='popover']").popover();

                    page('#pagination', json.pagecount, json.pageindex, json.pagesize, getThatDayItemList, '#pageNum');

                }else{
                    $.toast("没有查到数据",3000);
                    $('#item_list tbody').html('');
                    if($('#pagination').html().length > 0){
                        $('#pagination').jqPaginator('destroy');
                    }
                }
            }
        },
        complete:function(){
            $.progressBar().close();
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            $.toast('服务器未响应,请稍候重试',5000);
        }
    })
}
